from ctypes import *
import os
import platform

current_path = os.path.normpath(os.path.dirname(os.path.abspath(__file__)))

class GPIO:
    def __init__(self, deviceID=0) -> None:
        """
        搜索设备并选择设备，默认选择第一个设备

        Args:
            deviceID (int, optional): The ID of the device to select. Defaults to 0.
        """
        if(platform.system()=="Windows"):
            if "64bit" in platform.architecture():
                windll.LoadLibrary(current_path+"/libs/windows/x86_64/libusb-1.0.dll" )
                self.librockmong = windll.LoadLibrary(current_path+"/libs/windows/x86_64/librockmong.dll" )
            else:
                windll.LoadLibrary(current_path+"/libs/windows/x86/libusb-1.0.dll" )
                self.librockmong = windll.LoadLibrary(current_path+"/libs/windows/x86/librockmong.dll" )
        elif(platform.system()=="Darwin"):
            cdll.LoadLibrary(current_path+"/libs/mac_os/libusb-1.0.0.dylib" )
            self.librockmong = cdll.LoadLibrary(current_path+"/libs/mac_os/librockmong.dylib" )
        elif(platform.system()=="Linux"):
            if 'armv7' in platform.machine():
                cdll.LoadLibrary(current_path+"/libs/linux/armv7/libusb-1.0.so" )
                self.librockmong = cdll.LoadLibrary(current_path+"/libs/linux/armv7/librockmong.so" )
            elif 'mips64' in platform.machine():
                cdll.LoadLibrary(current_path+"/libs/linux/mips64/libusb-1.0.so" )
                self.librockmong = cdll.LoadLibrary(current_path+"/libs/linux/mips64/librockmong.so" )
            elif 'aarch64' in platform.machine():
                cdll.LoadLibrary(current_path+"/libs/linux/aarch64/libusb-1.0.so" )
                self.librockmong = cdll.LoadLibrary(current_path+"/libs/linux/aarch64/librockmong.so" )
            elif 'arm64' in platform.machine():
                cdll.LoadLibrary(current_path+"/libs/linux/arm64/libusb-1.0.so" )
                self.librockmong = cdll.LoadLibrary(current_path+"/libs/linux/arm64/librockmong.so" )
            else:
                if "64bit" in platform.architecture():
                    cdll.LoadLibrary(current_path+"/libs/linux/x86_64/libusb-1.0.so" )
                    self.librockmong = cdll.LoadLibrary(current_path+"/libs/linux/x86_64/librockmong.so" )
                else:
                    cdll.LoadLibrary(current_path+"/libs/linux/x86/libusb-1.0.so" )
                    self.librockmong = cdll.LoadLibrary(current_path+"/libs/linux/x86/librockmong.so" )

        SerialNumbers = (c_int * 20)()
        self.sn = 0
        # Scan device
        ret = self.UsbDevice_Scan(byref(SerialNumbers))
        if (0 > ret):
            print("Error: %d" % ret)
            exit()
        elif (ret == 0):
            print("No device!")
            exit()
        else:
            for i in range(ret):
                print("Dev%d SN: %d" % (i, SerialNumbers[i]))

        self.sn = SerialNumbers[deviceID]  # 选择设备，默认设备0

    def UsbDevice_Scan(self, SerialNumbers):
        """
        Scans for USB devices with the specified serial numbers.

        Args:
            SerialNumbers (list): A list of serial numbers of the USB devices to scan for.

        Returns:
            list: A list of USB devices found with the specified serial numbers.
        """
        return self.librockmong.UsbDevice_Scan(SerialNumbers)
    
    def IO_InitPin(self, Pin, Mode, Pull):
        """
        初始化引脚工作模式

        Parameters:
        - Pin (int): 引脚编号。0，P0. 1, P1...
        - Mode (int): 输入输出模式。0，输入。1，输出。2，开漏
        - Pull (int): 上拉下拉电阻。0，无。1，使能内部上拉。2，使能内部下拉

        """
        ret=self.librockmong.IO_InitPin(self.sn, Pin, Mode, Pull)
        if (0 > ret):
            print("error: %d" % ret)

    def IO_ReadPin(self, Pin):
        """
        读取引脚状态

        Parameters:
        Pin (int): 引脚编号。0，P0. 1, P1...

        Returns:
        PinState (int): 返回引脚状态。0，低电平。1，高电平
        """

        PinState = (c_int)()
        ret=self.librockmong.IO_ReadPin(self.sn, Pin, byref(PinState))
        if (0 > ret):
            print("error: %d" % ret)
        return PinState.value

    def IO_WritePin(self, Pin, PinState):
        """
        控制引脚输出状态

        Parameters:
        Pin (int): 引脚编号。0，P0. 1, P1...
        PinState (int): 引脚状态。0，低电平。1，高电平

        """
        ret=self.librockmong.IO_WritePin(self.sn, Pin, PinState)
        if (0 > ret):
            print("error: %d" % ret)


if __name__=='__main__':
    gpio=GPIO()
    print(gpio.UsbDevice_Scan())